package com.xiaoyu.aop.aspectj;

import com.xiaoyu.aop.Pointcut;
import com.xiaoyu.aop.PointcutAdvisor;
import org.aopalliance.aop.Advice;

/**
 * aspectJ表达式的advisor
 */
public class AspectJExpressionPointcutAdvisor implements PointcutAdvisor {
    private volatile AspectJExpressionPointcut pointcut;
    private Advice advice;
    private String expression;

    public void setExpression(String expression) {
        this.expression = expression;
    }

    public void setAdvice(Advice advice) {
        this.advice = advice;
    }

    @Override
    public Advice getAdvice() {
        return advice;
    }

    /**
     * 单例模式保证唯一
     * @return
     */
    @Override
    public Pointcut getPointcut() {
        if (pointcut == null) {
            synchronized (this) {
                if (pointcut == null) {
                    pointcut = new AspectJExpressionPointcut(expression);
                }
            }
        }
        return pointcut;
    }
}
